﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Common;

namespace BusinessLogic
{
    public class TaiKhoan
    {

        public Entities.TaiKhoan DangNhap(string taikhoan,string matkhau)
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            Entities.TaiKhoan tk = new Entities.TaiKhoan();
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_DangNhap";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                cmd.Parameters.Add("@TenDangNhap", SqlDbType.NVarChar, 200).Value = taikhoan;
                cmd.Parameters.Add("@MatKhau", SqlDbType.NVarChar, 200).Value = matkhau;

                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                if (reader.Read())
                {
                    tk.TenDangNhap= reader["TenDangNhap"].ToString();
                    tk.MatKhauDangNhap = reader["MatKhauDangNhap"].ToString();
                    tk.KhoaTaiKhoan = Boolean.Parse( reader["KhoaTaiKhoan"].ToString());
                    tk.TenNhanVien = reader["TenNhanVien"].ToString();
                    tk.MaNhanVien = reader["MaNhanVien"].ToString();
                    tk.Administrator = Boolean.Parse(reader["Administrator"].ToString());
                    tk.TenNhomQuyen = reader["TenNhomQuyen"].ToString();
                    LuuVietHeThong.LuuViet("Đăng nhập", tk.MaNhanVien, "N/A", tk.TenDangNhap, DateTime.Now.ToString("hh:mm dd/MM/yyyy"));
                    }
                else
                {
                    tk = null;
                }
            }
            catch (Exception)
            {

                tk = null;
            }
            
            return tk;
        }

        public int Insert(Entities.TaiKhoan tk)
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            int i = 0;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_TaiKhoan_Insert";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                cmd.Parameters.Add("@TenDangNhap", SqlDbType.NVarChar, 200).Value = tk.TenDangNhap;
                cmd.Parameters.Add("@MatKhauDangNhap", SqlDbType.NVarChar, 200).Value = tk.MatKhauDangNhap;
                cmd.Parameters.Add("@KhoaTaiKhoan", SqlDbType.Bit).Value = tk.KhoaTaiKhoan;
                cmd.Parameters.Add("@TenNhanVien", SqlDbType.NVarChar, 200).Value = tk.TenNhanVien;
                cmd.Parameters.Add("@MaNhanVien", SqlDbType.NVarChar, 20).Value = tk.MaNhanVien;
                cmd.Parameters.Add("@Administrator", SqlDbType.Bit).Value = tk.Administrator;
                cmd.Parameters.Add("@TenNhomQuyen", SqlDbType.NVarChar, 50).Value = tk.TenNhomQuyen;

                i = cmd.ExecuteNonQuery();

                LuuVietHeThong.LuuViet("Insert", tk.TK.MaNhanVien, "Thêm tài khoản: " + tk.TenDangNhap, tk.TK.TenDangNhap, DateTime.Now.ToString("hh:mm dd/MM/yyyy"));
            }
            catch (Exception)
            {

                i = -2;
            }
            finally
            {
                con.Close();
            }
            return i;
        }
        public int Update(Entities.TaiKhoan tk)
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            int i = 0;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_TaiKhoan_Update";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                cmd.Parameters.Add("@TenDangNhap", SqlDbType.NVarChar, 200).Value = tk.TenDangNhap;
                cmd.Parameters.Add("@MatKhauDangNhap", SqlDbType.NVarChar, 200).Value = tk.MatKhauDangNhap;
                cmd.Parameters.Add("@KhoaTaiKhoan", SqlDbType.Bit).Value = tk.KhoaTaiKhoan;
                cmd.Parameters.Add("@TenNhanVien", SqlDbType.NVarChar, 200).Value = tk.TenNhanVien;
                cmd.Parameters.Add("@MaNhanVien", SqlDbType.NVarChar, 20).Value = tk.MaNhanVien;
                cmd.Parameters.Add("@Administrator", SqlDbType.Bit).Value = tk.Administrator;
                cmd.Parameters.Add("@TenNhomQuyen", SqlDbType.NVarChar, 50).Value = tk.TenNhomQuyen;

                i = cmd.ExecuteNonQuery();

                LuuVietHeThong.LuuViet("Update", tk.TK.MaNhanVien, "Sửa tài khoản: " + tk.TenDangNhap, tk.TK.TenDangNhap, DateTime.Now.ToString("hh:mm dd/MM/yyyy"));

            }
            catch (Exception)
            {

                i = -2;
            }
            finally
            {
                con.Close();
            }
            return i;
        }
        public int Delete(Entities.TaiKhoan tk)
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            int i = 0;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_TaiKhoan_Delete";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                cmd.Parameters.Add("@TenDangNhap", SqlDbType.NVarChar, 200).Value = tk.TenDangNhap;

                i = cmd.ExecuteNonQuery();
                LuuVietHeThong.LuuViet("Delete", tk.TK.MaNhanVien, "Xóa tài khoản: "+tk.TenDangNhap, tk.TK.TenDangNhap, DateTime.Now.ToString("hh:mm dd/MM/yyyy"));

            }
            catch (Exception)
            {

                i = -2;
            }
            finally
            {
                con.Close();
            }
            return i;
        }

        public DataTable SelectAll()
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            DataTable dt = new DataTable();
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_TaiKhoan_SelectAll";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();

                adapter.Fill(ds,"TaiKhoan");
                dt = ds.Tables["TaiKhoan"];
            }
            catch
            {
                dt = null;
            }
            finally
            {
                con.Close();
            }
            return dt;
        }
        public Entities.TaiKhoan SelectByKey(string tendangnhap)
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            Entities.TaiKhoan tk = new Entities.TaiKhoan();
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_TaiKhoan_SelectByKey";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                cmd.Parameters.Add("@TenDangNhap", SqlDbType.NVarChar, 200).Value = tendangnhap;

                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                if (reader.Read())
                {
                    tk.TenDangNhap = reader["TenDangNhap"].ToString();
                    tk.MatKhauDangNhap = reader["MatKhauDangNhap"].ToString();
                    tk.KhoaTaiKhoan = Boolean.Parse(reader["KhoaTaiKhoan"].ToString());
                    tk.TenNhanVien = reader["TenNhanVien"].ToString();
                    tk.MaNhanVien = reader["MaNhanVien"].ToString();
                    tk.Administrator = Boolean.Parse(reader["Administrator"].ToString());
                    tk.TenNhomQuyen = reader["TenNhomQuyen"].ToString();
                    LuuVietHeThong.LuuViet("Đăng nhập", tk.MaNhanVien, "N/A", tk.TenDangNhap, DateTime.Now.ToString("hh:mm dd/MM/yyyy"));
                }
                else
                {
                    tk = null;
                }
            }
            catch
            {
                tk = null;
            }
            finally
            {
                con.Close();
            }
            return tk;
        }


    }
}
